#!D:\Perl\bin\perl.exe -w
use strict ;
use CGI;
use CGI::Carp ;

use constant BUFFER_SIZE =>4096;
use constant IMAGE_DIRECTORY => 'D:\wamp\bin\apache\Apache2.2.11\cgi-bin\images';

my $q = new CGI ;
my $buffer ="" ;

my $image = random_file(IMAGE_DIRECTORY,'\\.(jpg|gif|png)$');


my $type=$image=~/\.(\w+)$/;
$type eq "jpg" and $type = "jpeg" ;
print $q->header(-type=>"image/$type",-expires=>"-1d");

binmode STDOUT;
local *IMAGE; 
open IMAGE,IMAGE_DIRECTORY."/$image" or die "cannot open file $image\n" ;
while(read(IMAGE,$buffer,BUFFER_SIZE)){
print $buffer;
}
close IMAGE;

sub random_file
{
my ($dir,$mask)=@_ ;
my $i=0;
my $file;

opendir DIR,$dir or die "Cannot open $dir :$!";
while(defined ($_ = readdir DIR)){
/$mask/o or next if defined $mask ;
rand ++$i < 1 and $file = $_ ;
}
closedir DIR ;
return $file;
}